package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Employee;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceimpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
                Page<Category> categoryPage=categoryMapper.page(categoryPageQueryDTO);
        long total=categoryPage.getTotal();
        List<Category> list=categoryPage.getResult();
        return new PageResult(total,list);
    }

    @Override
    public void insert(CategoryDTO categoryDTO) {
        Category category=Category.builder().
                createTime(LocalDateTime.now()).
                updateTime(LocalDateTime.now()).
                createUser(BaseContext.getCurrentId()).
                updateUser(BaseContext.getCurrentId())
                .build();
        BeanUtils.copyProperties(categoryDTO,category);
        //默认禁用
        category.setStatus(0);
        categoryMapper.insert(category);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category=Category.builder().
                updateTime(LocalDateTime.now()).
                updateUser(BaseContext.getCurrentId())
                .build();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.update(category);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Category category=new Category();
        category.setStatus(status);
        category.setId(id);
       /* category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        categoryMapper.update(category);
    }

    @Override
    public void deleteById(Long id) {
        //判断有无关联
        Long count=dishMapper.count(id);
        if (count>0)
        {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        count=setmealMapper.count(id);
        if (count>0)
        {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.deleteById(id);
    }

    @Override
    public List<Category> selectType(Integer type) {
        CategoryPageQueryDTO categoryDTO=new CategoryPageQueryDTO();
        categoryDTO.setType(type);
        List<Category> list=categoryMapper.page(categoryDTO);
        return list;
    }
}
